<template>
	<view class="login">
		<swiper vertical="true" style="height: 100vh;">
			<swiper-item>
				<scroll-view>
					<view class="login-tel">
						<view class="tel-main">
							<!-- 关闭按钮 -->
							<view class="close" @click="back">
								<image class="close-img" src="../../static/image/close.png" mode=""></image>
							</view>
							<!-- logo -->
							<view class="logo">胖达优品</view>
							<!-- 注册按钮 -->
							<view class="register" @click="goRegister">手机号注册</view>
							<!-- 其他登录方式 -->
							<loginOhter></loginOhter>
							<!-- 去登录 -->
							<view class="login-go">
								<view>已有账号，去登录</view>
								<image src="../../static/image/xia.png" mode=""></image>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
			<swiper-item>
				<scroll-view>
					<view class="login-tel">
						<view class="tel-main">
							<view class="close">
								<view @click="back">
									<image src="../../static/image/close.png" mode=""></image>
								</view>
								<view class="center">
									<image src="../../static/image/shang.png" mode=""></image>
									<view>没有账号，去注册</view>
								</view>
								<view></view>
							</view>
							<view class="form">
								<view class="form-list">
									<view class="form-item">
										<view>账号</view>
										<input v-model="formData.userName" class='item-input' type="text" value=""
											placeholder="请输入手机号/昵称" />
									</view>
									<view class="form-item">
										<view>密码</view>
										<input v-model="formData.userPwd" class='item-input' type="password" value=""
											placeholder="请输入6-16位密码" />
										
									</view>
								</view>
								<view class="form-text">
									<view>忘记密码？</view>
									<view>免密登录</view>
								</view>
								<view class="register" @click="loginIsTrue()">登录</view>
								<view style="font-size: 20rpx;text-align: center;padding: 20rpx;" class="f-color">
									温馨提示：您可选择免密登录，方便快捷</view>
							</view>
							<loginOhter></loginOhter>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex'
	import $http from '@/common/api/request.js'
	import loginOhter from '@/components/login/login-other.vue'
	export default {
		data() {
			return {
				// 用户输入的信息
				formData: {
					userName: '',
					userPwd: '',
				},
			}
		},
		components: {
			loginOhter
		},
		onReady() {},
		methods: {

			...mapMutations(['login']),
			// 关闭当前页面返回上一页
			back() {
				uni.navigateTo({
					url: '../index/index'
				})
			},
			// 判断是否正确输入登录信息
			loginIsTrue() {
				if (this.formData.userName == '') {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					})
					return
				}
				if (this.formData.userPwd == '' || this.formData.userPwd.length<6 ||this.formData.userPwd.length>16) {
					uni.showToast({
						title: '请输入符合条件的密码',
						icon: 'none'
					})
					return
				}
				this.userLogin()
			},
			// 用户登录的方法
			userLogin() {
				$http.request({
					url: '/login',
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						userName: this.formData.userName,
						userPwd: this.formData.userPwd
					}
				}).then((res) => {
					if (res.success) {
						// 保存用户信息
						this.login(res.data)
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						uni.navigateTo({
							url:'../index/index'
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(() => {
					uni.showToast({
						title: '请求失败',
						icon: 'none'
					})
				})
			},
			// 跳转到注册页
			goRegister() {
				uni.navigateTo({
					url: '../phone-register/phone-register'
				})
			}
		}
	}
</script>

<style scoped>
	/* 第一页 */
	.login-tel {
		width: 100vw;
		max-width:790rpx ;
		height: 100vh;
	}

	.tel-main {
		padding: 20rpx;
	}

	.close-img {
		width: 50rpx;
		height: 50rpx;
	}

	.logo {
		padding: 300rpx 0;
		color: #00B7FF;
		font-size: 100rpx;
		text-align: center;
	}

	.register {
		width: 90%;
		padding: 26rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #00B7FF;
		color: #FFFFFF;
		border-radius: 50rpx;
		margin: 0 auto;
	}


	.login-go {
		padding-top: 100rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		font-size: 28rpx
	}

	.login-go image {
		padding-top: 40rpx;
		width: 50rpx;
		height: 50rpx;
	}

	/* 第二页 */
	/* 头部 */
	.close {
		display: flex;
		justify-content: space-between;
	}

	.close view {
		flex: 1;
	}

	.center {
		text-align: center;
		font-size: 28rpx;
	}

	.close image {
		width: 50rpx;
		height: 50rpx;
	}

	/* 表单 */
	.form {
		padding-top: 300rpx;
	}

	.form-item {
		padding: 20rpx;
		display: flex;
		border-bottom: 2rpx solid #A1A1A1;
	}

	.item-input {
		padding-left: 20rpx;
	}

	.item-text {
		width: 100rpx;
	}

	.form-text {
		padding: 30rpx 20rpx;
		font-size: 28rpx;
		display: flex;
		justify-content: space-between;
	}
</style>
